/********************************************************************************
 * Copyright(c) 2020-2025 VINCENT_WY All rights reserved.
 * - Filename
 * - Author  Vincent
 * - Version V1.0.0
 * - Date    2023/09/21
 * - Brief
 * - FunctionList:
 ******************************************************************************
 * History:
 *
 *
 *
 ******************************************************************************
 */

#pragma once

#include <condition_variable>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <mutex>
#include <string>
#include <thread>
#include <vector>
#include <sys/stat.h>
#include <dirent.h>
#ifndef DISABLE_CYBER
#include "cyber/cyber.h"
#endif
#include "can.pb.h"
#include "bavm.h"
#include "TimeUtil.h"

class NoBufferedCanMsgReplayImpl
{
public:
  NoBufferedCanMsgReplayImpl(const bool &enbale, const std::string &path);
  ~NoBufferedCanMsgReplayImpl();
  void start(const long long int &time);
  void stop();
  bool isComplete();
  void doRepeat();
  long long int getFirstFrameTm();

private:
  enum class Status
  {
    START = 0,
    REPEAT,
    CONTINUE,
    IDLE
  };

  std::string Tag{"[NoBufferedCanMsgReplayImpl]:"};
  bool _isExit{false};
  bool _usrCanReplay{true};
  bool _isComplete{false};
  long long int _startTime{0};
  std::string _root{};
  std::mutex _frameMtx;
  std::condition_variable _frameCv;
  std::deque<Bavm::CanMsg> _frameDqu;
  std::vector<std::pair<long long int, std::string>> _canFileList;
  Status _status{Status::IDLE};
  std::unique_ptr<std::thread> _readLoop{nullptr};
  std::unique_ptr<std::thread> _invokeLoop{nullptr};
  #ifndef DISABLE_CYBER
  std::unique_ptr<apollo::cyber::Node> _canMsgTalkerNode{nullptr};
  std::shared_ptr<apollo::cyber::Writer<autoplt::can>> _canMsgTalker{nullptr};
  #endif
  void ReadFile();
  void ReadLoop();
  void InvokeLoop();
};